import { getRoleListApi } from '@/api/system/role/role'
import { RoleListParam } from '@/api/system/role/RoleModel'
import { StatusCode } from '@/http/request'
import { nextTick, onMounted, reactive, ref } from 'vue'

export default function useRoleTable() {
    // 表格高度
    const tableHeight = ref(0)

    // 定义表格数据
    const roleTable = reactive({
        list: []
    })

    // 定义列表查询的参数
    const listParam = reactive<RoleListParam>({
        current_page: 1,
        page_size: 20,
        role_name: '',
        status: '1',
        total: 0
    })

    // 获取角色列表
    const getRoleList = async () => {
        let res = await getRoleListApi(listParam)
        if (res && res.code == StatusCode.Success) {
            console.log('角色列表：', res)
            roleTable.list = res.data.list
            listParam.total = res.data.total
        }
    }
    // 页容量改变时触发
    const sizeChange = (size: number) => {
        listParam.page_size = size
        getRoleList()
    }

    // 页数改变时触发
    const currentChange = (page: number) => {
        listParam.current_page = page
        getRoleList()
    }

    // 搜索
    const searchBtn = () => {
        getRoleList()
    }
    // 重置
    const resetBtn = () => {
        listParam.role_name = ''
        listParam.status = '1'
        listParam.current_page = 1
        getRoleList()
    }

    // 首次加载
    onMounted(() => {
        getRoleList()
        nextTick(() => {
            tableHeight.value = window.innerHeight - 210
        })
    })

    return {
        tableHeight,
        roleTable,
        listParam,
        getRoleList,
        sizeChange,
        currentChange,
        searchBtn,
        resetBtn
    }
}
